package com.example.jmm.myapplication;

import android.Manifest;
import android.graphics.SurfaceTexture;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private Button previewBtn;
    private TextureView viewTextureview;
    private Surface mPreviewSurface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,
                    }
                    , 1);
        }

        setContentView(R.layout.activity_main);
        previewBtn = (Button) findViewById(R.id.PreviewBtn);
        viewTextureview = (TextureView) findViewById(R.id.previewSurface);

    }

    public void viewBtnClick(View view) {
        final SurfaceTexture viewTextureSurface = viewTextureview.getSurfaceTexture();
        mPreviewSurface = new Surface(viewTextureSurface);
        Log.d(TAG, "viewBtnClick: ======================setPreviewDisplay(mPreviewSurface);");
        setPreviewDisplay(mPreviewSurface);
        Log.d(TAG, "viewBtnClick: ======================startPreview();");
        startPreview();
    }

    public void setPreviewDisplay(Surface surface) {
        nativeSetPreviewDisplay(surface);
    }

    public void startPreview() {
        nativeStartPreview();
    }

    static {
        System.loadLibrary("h264disp");
        System.loadLibrary("h264Preview");
    }

    private native void nativeSetPreviewDisplay(final Surface surface);
    private native void nativeStartPreview();
}
